from django.db import transaction
from drf_spectacular.utils import extend_schema
from rest_framework import status, filters
from rest_framework.decorators import action
from rest_framework.response import Response

from apps.clsbb.filters import *
from apps.clsbb.model.check_models import *
from apps.clsbb.serializers import *
from apps.pam.filters import Pam_business_opportunity_RecordsFilter, Pam_Contract_tobesigned_RecordsFilter, \
    Pam_contract_management_RecordsFilter, Pam_framecontract_order_RecordsFilter, \
    Pam_framecontract_order_whole_RecordsFilter, Pam_singlecontract_order_whole_RecordsFilter, \
    Pam_supplier_management_RecordsFilter
from apps.pam.models import Pam_business_opportunity_Records, Pam_Progress_log, Pam_business_opportunityCheck, \
    Pam_Contract_tobesigned_Records, Pam_Contract_tobesignedCheck, Pam_Contract_tobesigned_Progress_log, \
    Pam_contract_management_Records, Pam_contract_managementCheck, Pam_framecontract_order_Records, \
    Pam_framecontract_orderCheck, Pam_framecontract_order_whole_Records, Pam_framecontract_order_wholeCheck, \
    Pam_singlecontract_order_whole_Records, Pam_singlecontract_order_wholeCheck, Pam_supplier_management_Records
from apps.pam.serializers import Pam_business_opportunity_RecordsSerializer, Pam_Contract_tobesigned_RecordsSerializer, \
    Pam_contract_management_RecordsSerializer, Pam_framecontract_order_RecordsSerializer, \
    Pam_framecontract_order_whole_RecordsSerializer, Pam_singlecontract_order_whole_RecordsSerializer, \
    Pam_supplier_management_RecordsSerializer
from apps.system.models import Region
from extensions.permissions import IsAuthenticated
from extensions.viewsets import *
from django.db.models import Count, Q
from apps.common.models import Pagination

# Create your views here.
# 项目台账管理系统
# Project account management system


class Pam_business_opportunity_RecordsViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """七库全书"""
    #获取queryset



    queryset = Pam_business_opportunity_Records.objects.all()
    serializer_class = Pam_business_opportunity_RecordsSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = Pam_business_opportunity_RecordsFilter
    search_fields = ['number','name','attribute_of_first_party__name']
    select_related_fields = ['company', 'filled_by', 'next_checker',]
    # prefetch_related_fields = ['check_records','downloads_records','views_records']
    # prefetch_related_fields = ['check_records',]
    ordering_fields = ['id', 'name','is_effective','add_time']
    #
    def list(self, request):
        queryset = self.filter_queryset(self.get_queryset())
        statistic = queryset.aggregate(count=Count('id'),total_budget=Sum('budget'),)
        serializer = self.get_serializer(queryset, many=True)
        # 遍历出每个item的region_name
        queryset_Region=Region.objects.all()
        for item in serializer.data:
            shi=queryset_Region.filter(id=item['region'][0])[0].region_name
            xian=queryset_Region.filter(id=item['region'][1])[0].region_name
            item['region_name']=shi+'/'+xian
        data={
            "count": statistic['count'],
            "total_budget": statistic['total_budget'],
            "results":serializer.data
        }
        return Response(data=data, status=status.HTTP_200_OK)

    def add_check_record(self,serializer):
        if 'opinion' in self.request.data:
            pam_business_opportunity = serializer.save()
            pam_business_opportunity_check = Pam_business_opportunityCheck.objects.create(
                pam_business_opportunity=pam_business_opportunity, check_user=self.user,
                opinion=self.request.data['opinion'],
                is_agree=self.request.data['is_agree'])
            print(pam_business_opportunity_check)

    def add_progress_record(self,serializer):
        if 'progress' in self.request.data:
            pam_business_opportunity = serializer.save()
            pam_business_opportunity_log = Pam_Progress_log.objects.create(
                pam_business_opportunity=pam_business_opportunity, filled_by=self.user,
                progress=self.request.data['progress'])
            print(pam_business_opportunity_log)

    @transaction.atomic
    def perform_create(self, serializer):
        pam_business_opportunity = serializer.save()
        #添加进展情况记录
        self.add_progress_record(serializer)
        # 添加审核情况记录
        self.add_check_record(serializer)


    @transaction.atomic
    def perform_update(self, serializer):
        # 添加进展情况记录
        self.add_progress_record(serializer)
        # 添加审核情况记录
        self.add_check_record(serializer)

    @transaction.atomic
    def partial_update(self, request, *args, **kwargs):
        instance =Pam_business_opportunity_Records.objects.filter(id=self.request.data['id'])
        instance.update(del_flag=self.request.data['del_flag'])
        kwargs['partial'] = True
        return super().update(request, *args, **kwargs)

    @action(detail=False, methods=['get'])
    def waitMeModify(self, request, *args, **kwargs):
        """待我修改"""
        query =Q(filled_by=self.user) & Q(is_effective=-1) & Q(del_flag=0)
        queryset = Pam_business_opportunity_Records.objects.filter(query)
        result = queryset.aggregate(
            total_count=Count('name')
        )
        return Response(data=result, status=status.HTTP_200_OK)

    @action(detail=False, methods=['get'])
    def waitMeCheck(self, request, *args, **kwargs):
        """待我审核"""
        query =Q(next_checker=self.user) & Q(is_effective=0) & Q(del_flag=0)
        queryset = Pam_business_opportunity_Records.objects.filter(query)
        result = queryset.aggregate(
            total_count=Count('name')
        )
        return Response(data=result, status=status.HTTP_200_OK)

    def get_pdfinstance(self,Clsbbfak_item,pdf_type):
        if pdf_type in Clsbbfak_item:
            query = Q(id=Clsbbfak_item[pdf_type])
            Clsbbfak_item[pdf_type] = Clsbb_Common_Pdfs.objects.filter(query)[0]
            return Clsbbfak_item[pdf_type]
        return None


class Pam_Contract_tobesigned_RecordsViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """七库全书"""
    queryset = Pam_Contract_tobesigned_Records.objects.all()
    serializer_class = Pam_Contract_tobesigned_RecordsSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = Pam_Contract_tobesigned_RecordsFilter
    search_fields = ['number','name','attribute_of_first_party__name']
    select_related_fields = ['company', 'filled_by', 'next_checker',]
    # prefetch_related_fields = ['check_records','downloads_records','views_records']
    # prefetch_related_fields = ['check_records',]
    ordering_fields = ['id', 'name','is_effective','add_time']
    #
    def list(self, request):
        queryset = self.filter_queryset(self.get_queryset())
        statistic = queryset.aggregate(count=Count('id'),total_budget=Sum('budget'),)
        serializer = self.get_serializer(queryset, many=True)
        # 遍历出每个item
        queryset_Region=Region.objects.all()
        for item in serializer.data:
            shi=queryset_Region.filter(id=item['region'][0])[0].region_name
            xian=queryset_Region.filter(id=item['region'][1])[0].region_name
            item['region_name']=shi+'/'+xian
        data={
            "count": statistic['count'],
            "total_budget": statistic['total_budget'],
            "results":serializer.data
        }
        return Response(data=data, status=status.HTTP_200_OK)

    def add_check_record(self,serializer):
        if 'opinion' in self.request.data:
            pam_Contract_tobesigned = serializer.save()
            pam_Contract_tobesigned_check = Pam_Contract_tobesignedCheck.objects.create(
                pam_Contract_tobesigned=pam_Contract_tobesigned, check_user=self.user,
                opinion=self.request.data['opinion'],
                is_agree=self.request.data['is_agree'])
            print(pam_Contract_tobesigned_check)

    def add_progress_record(self,serializer):
        if 'progress' in self.request.data:
            pam_Contract_tobesigned = serializer.save()
            pam_Contract_tobesigned_log = Pam_Contract_tobesigned_Progress_log.objects.create(
                pam_Contract_tobesigned=pam_Contract_tobesigned, filled_by=self.user,
                progress=self.request.data['progress'])
            print(pam_Contract_tobesigned_log)

    @transaction.atomic
    def perform_create(self, serializer):
        pam_Contract_tobesigned = serializer.save()
        #添加进展情况记录
        self.add_progress_record(serializer)
        # 添加审核情况记录
        self.add_check_record(serializer)


    @transaction.atomic
    def perform_update(self, serializer):
        # 添加进展情况记录
        self.add_progress_record(serializer)
        # 添加审核情况记录
        self.add_check_record(serializer)

    @transaction.atomic
    def partial_update(self, request, *args, **kwargs):
        instance =Pam_Contract_tobesigned_Records.objects.filter(id=self.request.data['id'])
        instance.update(del_flag=self.request.data['del_flag'])
        kwargs['partial'] = True
        return super().update(request, *args, **kwargs)

    @action(detail=False, methods=['get'])
    def waitMeModify(self, request, *args, **kwargs):
        """待我修改"""
        query =Q(filled_by=self.user) & Q(is_effective=-1) & Q(del_flag=0)
        queryset = Pam_Contract_tobesigned_Records.objects.filter(query)
        result = queryset.aggregate(
            total_count=Count('name')
        )
        return Response(data=result, status=status.HTTP_200_OK)

    @action(detail=False, methods=['get'])
    def waitMeCheck(self, request, *args, **kwargs):
        """待我审核"""
        query =Q(next_checker=self.user) & Q(is_effective=0) & Q(del_flag=0)
        queryset = Pam_Contract_tobesigned_Records.objects.filter(query)
        result = queryset.aggregate(
            total_count=Count('name')
        )
        return Response(data=result, status=status.HTTP_200_OK)

    def get_pdfinstance(self,Clsbbfak_item,pdf_type):
        if pdf_type in Clsbbfak_item:
            query = Q(id=Clsbbfak_item[pdf_type])
            Clsbbfak_item[pdf_type] = Clsbb_Common_Pdfs.objects.filter(query)[0]
            return Clsbbfak_item[pdf_type]
        return None


class Pam_contract_management_RecordsViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """七库全书"""
    queryset = Pam_contract_management_Records.objects.all()
    serializer_class = Pam_contract_management_RecordsSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = Pam_contract_management_RecordsFilter
    search_fields = ['number','erp_number','name','attribute_of_first_party__name']
    select_related_fields = ['company', 'filled_by', 'next_checker',]
    # prefetch_related_fields = ['check_records','downloads_records','views_records']
    # prefetch_related_fields = ['check_records',]
    ordering_fields = ['id', 'name','is_effective','add_time']
    #
    def list(self, request):
        queryset = self.filter_queryset(self.get_queryset())
        statistic = queryset.aggregate(count=Count('id'),
                                       total_contract_amount=Sum('contract_amount'),
                                       total_estimated_cash_amount=Sum('estimated_cash_amount'),
                                       )
        serializer = self.get_serializer(queryset, many=True)
        # 遍历出每个item
        queryset_Region=Region.objects.all()
        for item in serializer.data:
            shi=queryset_Region.filter(id=item['region'][0])[0].region_name
            xian=queryset_Region.filter(id=item['region'][1])[0].region_name
            item['region_name']=shi+'/'+xian
        data={
            "count": statistic['count'],
            "total_contract_amount": statistic['total_contract_amount'],
            "total_estimated_cash_amount": statistic['total_estimated_cash_amount'],
            "results":serializer.data
        }
        return Response(data=data, status=status.HTTP_200_OK)

    def add_check_record(self,serializer):
        if 'opinion' in self.request.data:
            pam_contract_management = serializer.save()
            pam_contract_management_check = Pam_contract_managementCheck.objects.create(
                pam_contract_management=pam_contract_management, check_user=self.user,
                opinion=self.request.data['opinion'],
                is_agree=self.request.data['is_agree'])
            print(pam_contract_management_check)


    @transaction.atomic
    def perform_create(self, serializer):
        pam_contract_management = serializer.save()
        # 添加审核情况记录
        self.add_check_record(serializer)


    @transaction.atomic
    def perform_update(self, serializer):
        # 添加审核情况记录
        self.add_check_record(serializer)

    @transaction.atomic
    def partial_update(self, request, *args, **kwargs):
        instance =Pam_contract_management_Records.objects.filter(id=self.request.data['id'])
        instance.update(del_flag=self.request.data['del_flag'])
        kwargs['partial'] = True
        return super().update(request, *args, **kwargs)

    @action(detail=False, methods=['get'])
    def waitMeModify(self, request, *args, **kwargs):
        """待我修改"""
        query =Q(filled_by=self.user) & Q(is_effective=-1) & Q(del_flag=0)
        queryset = Pam_contract_management_Records.objects.filter(query)
        result = queryset.aggregate(
            total_count=Count('name')
        )
        return Response(data=result, status=status.HTTP_200_OK)

    @action(detail=False, methods=['get'])
    def waitMeCheck(self, request, *args, **kwargs):
        """待我审核"""
        query =Q(next_checker=self.user) & Q(is_effective=0) & Q(del_flag=0)
        queryset = Pam_contract_management_Records.objects.filter(query)
        result = queryset.aggregate(
            total_count=Count('name')
        )
        return Response(data=result, status=status.HTTP_200_OK)

    def get_pdfinstance(self,Clsbbfak_item,pdf_type):
        if pdf_type in Clsbbfak_item:
            query = Q(id=Clsbbfak_item[pdf_type])
            Clsbbfak_item[pdf_type] = Clsbb_Common_Pdfs.objects.filter(query)[0]
            return Clsbbfak_item[pdf_type]
        return None


class Pam_framecontract_order_RecordsViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """七库全书"""
    queryset = Pam_framecontract_order_Records.objects.all()
    serializer_class = Pam_framecontract_order_RecordsSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = Pam_framecontract_order_RecordsFilter
    search_fields = ['number','name','pam_contract_management__erp_number','pam_contract_management__name']
    select_related_fields = ['company', 'filled_by', 'next_checker']
    # prefetch_related_fields = ['check_records','downloads_records','views_records']
    # prefetch_related_fields = ['check_records',]
    ordering_fields = ['id', 'name','is_effective','add_time']
    #
    def list(self, request):
        queryset = self.filter_queryset(self.get_queryset())
        ip_address = request.META.get('REMOTE_ADDR')
        user_agent = request.META.get('HTTP_USER_AGENT')
        print('ip_address:',ip_address)
        print('user_agent:',user_agent)
        statistic = queryset.aggregate(count=Count('id'),
                                       total_order_amount=Sum('order_amount'),
                                       total_order_estimated_cash_amount=Sum('order_estimated_cash_amount'),
                                       )
        serializer = self.get_serializer(queryset, many=True)
        data={
            "count": statistic['count'],
            "total_order_amount": statistic['total_order_amount'],
            "total_order_estimated_cash_amount": statistic['total_order_estimated_cash_amount'],
            "results":serializer.data
        }
        return Response(data=data, status=status.HTTP_200_OK)

    def add_check_record(self,serializer):
        if 'opinion' in self.request.data:
            pam_framecontract_order = serializer.save()
            pam_framecontract_order_check = Pam_framecontract_orderCheck.objects.create(
                pam_framecontract_order=pam_framecontract_order, check_user=self.user,
                opinion=self.request.data['opinion'],
                is_agree=self.request.data['is_agree'])
            print(pam_framecontract_order_check)


    @transaction.atomic
    def perform_create(self, serializer):
        pam_framecontract_order = serializer.save()
        # 添加审核情况记录
        self.add_check_record(serializer)


    @transaction.atomic
    def perform_update(self, serializer):
        # 添加审核情况记录
        self.add_check_record(serializer)

    @transaction.atomic
    def partial_update(self, request, *args, **kwargs):
        instance =Pam_framecontract_order_Records.objects.filter(id=self.request.data['id'])
        instance.update(del_flag=self.request.data['del_flag'])
        kwargs['partial'] = True
        return super().update(request, *args, **kwargs)

    @action(detail=False, methods=['get'])
    def waitMeModify(self, request, *args, **kwargs):
        """待我修改"""
        query =Q(filled_by=self.user) & Q(is_effective=-1) & Q(del_flag=0)
        queryset = Pam_framecontract_order_Records.objects.filter(query)
        result = queryset.aggregate(
            total_count=Count('name')
        )
        return Response(data=result, status=status.HTTP_200_OK)

    @action(detail=False, methods=['get'])
    def waitMeCheck(self, request, *args, **kwargs):
        """待我审核"""
        query =Q(next_checker=self.user) & Q(is_effective=0) & Q(del_flag=0)
        queryset = Pam_framecontract_order_Records.objects.filter(query)
        result = queryset.aggregate(
            total_count=Count('name')
        )
        return Response(data=result, status=status.HTTP_200_OK)

    def get_pdfinstance(self,Clsbbfak_item,pdf_type):
        if pdf_type in Clsbbfak_item:
            query = Q(id=Clsbbfak_item[pdf_type])
            Clsbbfak_item[pdf_type] = Clsbb_Common_Pdfs.objects.filter(query)[0]
            return Clsbbfak_item[pdf_type]
        return None


class Pam_framecontract_order_whole_RecordsViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """七库全书"""
    queryset = Pam_framecontract_order_whole_Records.objects.all()
    serializer_class = Pam_framecontract_order_whole_RecordsSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = Pam_framecontract_order_whole_RecordsFilter
    search_fields = ['number','name',
                     'pam_framecontract_order__number',
                     'pam_framecontract_order__name',
                     'pam_framecontract_order__pam_contract_management__name',
                     'pam_framecontract_order__pam_contract_management__erp_number',
                     ]
    select_related_fields = ['pam_framecontract_order','filled_by', 'next_checker']
    # prefetch_related_fields = ['check_records','downloads_records','views_records']
    # prefetch_related_fields = ['check_records',]
    ordering_fields = ['id', 'name','is_effective','add_time']

    def list(self, request):
        queryset = self.filter_queryset(self.get_queryset())
        statistic = queryset.aggregate(count=Count('id'),)
        serializer = self.get_serializer(queryset, many=True)
        sum_of_order_amount = sum(item['pam_framecontract_orders_items']['order_amount'] for item in serializer.data)
        sum_of_order_estimated_cash_amount = sum(
            item['pam_framecontract_orders_items']['order_estimated_cash_amount'] for item in serializer.data)
        sum_of_contract_amount = sum(
            item['pam_contract_managements_items']['contract_amount'] for item in serializer.data)
        data={
            "count": statistic['count'],
            "total_order_amount": sum_of_order_amount,
            "total_order_estimated_cash_amount": sum_of_order_estimated_cash_amount,
            "results":serializer.data
        }
        return Response(data=data, status=status.HTTP_200_OK)

    def add_check_record(self,serializer):
        if 'opinion' in self.request.data:
            pam_framecontract_order_whole = serializer.save()
            pam_framecontract_order_whole_check = Pam_framecontract_order_wholeCheck.objects.create(
                pam_framecontract_order_whole=pam_framecontract_order_whole, check_user=self.user,
                opinion=self.request.data['opinion'],
                is_agree=self.request.data['is_agree'])


    @transaction.atomic
    def perform_create(self, serializer):
        pam_framecontract_order = serializer.save()
        # 添加审核情况记录
        self.add_check_record(serializer)


    @transaction.atomic
    def perform_update(self, serializer):
        # 添加审核情况记录
        self.add_check_record(serializer)

    @transaction.atomic
    def partial_update(self, request, *args, **kwargs):
        instance =Pam_framecontract_order_whole_Records.objects.filter(id=self.request.data['id'])
        instance.update(del_flag=self.request.data['del_flag'])
        kwargs['partial'] = True
        return super().update(request, *args, **kwargs)

    @action(detail=False, methods=['get'])
    def waitMeModify(self, request, *args, **kwargs):
        """待我修改"""
        query =Q(filled_by=self.user) & Q(is_effective=-1) & Q(del_flag=0)
        queryset = Pam_framecontract_order_whole_Records.objects.filter(query)
        result = queryset.aggregate(
            total_count=Count('name')
        )
        return Response(data=result, status=status.HTTP_200_OK)

    @action(detail=False, methods=['get'])
    def waitMeCheck(self, request, *args, **kwargs):
        """待我审核"""
        query =Q(next_checker=self.user) & Q(is_effective=0) & Q(del_flag=0)
        queryset = Pam_framecontract_order_whole_Records.objects.filter(query)
        result = queryset.aggregate(
            total_count=Count('name')
        )
        return Response(data=result, status=status.HTTP_200_OK)

    def get_pdfinstance(self,Clsbbfak_item,pdf_type):
        if pdf_type in Clsbbfak_item:
            query = Q(id=Clsbbfak_item[pdf_type])
            Clsbbfak_item[pdf_type] = Clsbb_Common_Pdfs.objects.filter(query)[0]
            return Clsbbfak_item[pdf_type]
        return None


class Pam_singlecontract_order_whole_RecordsViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """七库全书"""
    queryset = Pam_singlecontract_order_whole_Records.objects.all()
    serializer_class = Pam_singlecontract_order_whole_RecordsSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = Pam_singlecontract_order_whole_RecordsFilter
    search_fields = ['number','name',
                     'pam_contract_management__name',
                     'pam_contract_management__erp_number',
                     ]
    select_related_fields = ['pam_contract_management','filled_by', 'next_checker']
    # prefetch_related_fields = ['check_records','downloads_records','views_records']
    # prefetch_related_fields = ['check_records',]
    ordering_fields = ['id', 'name','is_effective','add_time']

    def list(self, request):
        queryset = self.filter_queryset(self.get_queryset())
        statistic = queryset.aggregate(count=Count('id'),)
        serializer = self.get_serializer(queryset, many=True)
        sum_of_contract_amount = sum(item['pam_contract_managements_items']['contract_amount'] for item in serializer.data)
        data={
            "count": statistic['count'],
            "total_contract_amount": sum_of_contract_amount,
            "results":serializer.data
        }
        return Response(data=data, status=status.HTTP_200_OK)

    def add_check_record(self,serializer):
        if 'opinion' in self.request.data:
            pam_singlecontract_order_whole = serializer.save()
            pam_singlecontract_order_whole_check = Pam_singlecontract_order_wholeCheck.objects.create(
                pam_singlecontract_order_whole=pam_singlecontract_order_whole, check_user=self.user,
                opinion=self.request.data['opinion'],
                is_agree=self.request.data['is_agree'])


    @transaction.atomic
    def perform_create(self, serializer):
        pam_singlecontract_order = serializer.save()
        # 添加审核情况记录
        self.add_check_record(serializer)


    @transaction.atomic
    def perform_update(self, serializer):
        # 添加审核情况记录
        self.add_check_record(serializer)

    @transaction.atomic
    def partial_update(self, request, *args, **kwargs):
        instance =Pam_singlecontract_order_whole_Records.objects.filter(id=self.request.data['id'])
        instance.update(del_flag=self.request.data['del_flag'])
        kwargs['partial'] = True
        return super().update(request, *args, **kwargs)

    @action(detail=False, methods=['get'])
    def waitMeModify(self, request, *args, **kwargs):
        """待我修改"""
        query =Q(filled_by=self.user) & Q(is_effective=-1) & Q(del_flag=0)
        queryset = Pam_singlecontract_order_whole_Records.objects.filter(query)
        result = queryset.aggregate(
            total_count=Count('name')
        )
        return Response(data=result, status=status.HTTP_200_OK)

    @action(detail=False, methods=['get'])
    def waitMeCheck(self, request, *args, **kwargs):
        """待我审核"""
        query =Q(next_checker=self.user) & Q(is_effective=0) & Q(del_flag=0)
        queryset = Pam_singlecontract_order_whole_Records.objects.filter(query)
        result = queryset.aggregate(
            total_count=Count('name')
        )
        return Response(data=result, status=status.HTTP_200_OK)

    def get_pdfinstance(self,Clsbbfak_item,pdf_type):
        if pdf_type in Clsbbfak_item:
            query = Q(id=Clsbbfak_item[pdf_type])
            Clsbbfak_item[pdf_type] = Clsbb_Common_Pdfs.objects.filter(query)[0]
            return Clsbbfak_item[pdf_type]
        return None

class Pam_supplier_management_RecordsViewSet(ModelViewSet,ImportMixin):
    """Pam供应商信息"""
    serializer_class = Pam_supplier_management_RecordsSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = Pam_supplier_management_RecordsFilter
    search_fields = ['number','resource_pool','supplier_name','supplier_contact','supplier_contact_phone']
    select_related_fields = ['create_by', 'update_by']
    ordering_fields = ['id', 'number', 'create_time', 'update_time']
    queryset = Pam_supplier_management_Records.objects.all()

